package experiment.e06.code;

import java.util.*;

/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/10/20 15:49
 * @package experiment.e06.code
 * @description 实验二
 * 对例题11.8中的book类，创建10个相应的对象，
 * 分别用LinkedList、ArrayList、HashSet、HashMap类的对象来存储这10个对象，
 * 遍历输出所有对象。
 */

public class Ch02 {
    public static void main(String[] args) {
        // 创建 book 数组
        Book1[] books = new Book1[10];
        books[0] = new Book1("123-322-323", "活着", "余华");
        books[1] = new Book1("123-827-281", "兄弟", "余华");
        books[2] = new Book1("872-291-298", "在风雨中呐喊", "余华");
        books[3] = new Book1("425-222-212", "许三观卖血记", "余华");
        books[4] = new Book1("198-322-323", "一地一鸡毛", "刘震云");
        books[5] = new Book1("987-322-323", "一句顶一万句", "刘震云");
        books[6] = new Book1("123-217-213", "地坛", "史铁生");
        books[7] = new Book1("876-896-923", "张三普法", "大罗翔");
        books[8] = new Book1("987-282-920", "我的", "余华");
        books[9] = new Book1("987-272-292", "还活着", "余华");

        // =================== LinkedList ===================
        System.out.println("========== LinkedList =========");
        Collection<Book1> bookLinkedList = new LinkedList<>();
        // 添加元素
        for (int i = 0; i < books.length; i++) {
            bookLinkedList.add(books[i]);
        }
        // 迭代
        Iterator<Book1> iterator = bookLinkedList.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        // =================== ArrayList ===================
        System.out.println("========== ArrayList =========");
        Collection<Book1> bookArrayList = new ArrayList<>();
        bookArrayList.addAll(bookLinkedList);
        // 迭代
        iterator = bookArrayList.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        // =================== HashSet ===================
        System.out.println("========== HashSet =========");
        Collection<Book1> bookHashSet = new HashSet<>();
        bookHashSet.addAll(bookLinkedList);
        // 迭代
        iterator = bookArrayList.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        // =================== HashMap ===================
        System.out.println("========== HashMap =========");
        Map<String, Book1> bookHaspMap = new HashMap<>();
        // 添加元素
        for (int i = 0; i < books.length; i++) {
            bookHaspMap.put(books[i].getIsbn(), books[i]);
        }
        // 迭代
        Iterator<String> isbnIterator = bookHaspMap.keySet().iterator();
        while (isbnIterator.hasNext()) {
            String next = isbnIterator.next();
            System.out.println(next + "--->" + bookHaspMap.get(next));
        }
    }
}

/**
 * book 类
 */
class Book1 {
    private String isbn;
    private String title;
    private String author;

    public Book1(String isbn, String title, String author) {
        this.isbn = isbn;
        this.title = title;
        this.author = author;
    }

    public String getIsbn() {
        return isbn;
    }


    public String getTitle() {
        return title;
    }


    public String getAuthor() {
        return author;
    }


    @Override
    public String toString() {
        return "Book{" +
                "isbn='" + isbn + '\'' +
                ", title='" + title + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}